import {Injectable} from '@angular/core';
import {Employee} from '../types/resp/Employee';
import {EmployeeForm} from '../types/req/EmployeeForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {EmployeeQueryForm} from '../types/req/EmployeeQueryForm';

@Injectable({
  providedIn: 'root'
})
export class EmployeeService {
  employees: Employee[] = [
    {"id":1,"enterpriseId":4,"enterpriseName":"绿源农业有限公司","employeeNumber":"LYNY001","name":"张伟","password":"zhangwei123","gender":1,"birthDate":"1990-06-23","contactPhone":"13812345678","email":"zhangwei@greenfarm.com","province": "44", "provinceName": "广东省", "city": "4403", "cityName": "深圳市", "area": "440305", "areaName": "南山区", "address": "科技园南区1号","hireDate":"2024-10-02","departmentId":1,"departmentName":"市场营销部","positionId":3,"positionName":"市场经理","createTime":"2024-10-24 22:24:09","modifyTime":"2024-06-25 22:24:09"},
    {"id":2,"enterpriseId":1,"enterpriseName":"阳光科技有限公司","employeeNumber":"YGKJ002","name":"李娜","password":"lina456","gender":2,"birthDate":"1992-02-14","contactPhone":"13987654321","email":"lina@sunshine-tech.com","province": "11", "provinceName": "北京市", "city": "1101", "cityName": "北京市", "area": "110108", "areaName": "海淀区","address":"中关村大街1号","hireDate":"2024-12-27","departmentId":4,"departmentName":"财务部","positionId":3,"positionName":"财务分析师","createTime":"2024-08-10 22:24:09","modifyTime":"2024-10-05 22:24:09"},
    {"id":3,"enterpriseId":2,"enterpriseName":"蓝天环保有限公司","employeeNumber":"LTHB003","name":"王强","password":"wangqiang789","gender":1,"birthDate":"1988-05-28","contactPhone":"13712345678","email":"wangqiang@bluesky-env.com","province": "31", "provinceName": "上海市", "city": "3101", "cityName": "上海市", "area": "310115", "areaName": "浦东新区","address":"张江高科技园区456号","hireDate":"2024-03-28","departmentId":3,"departmentName":"环境工程部","positionId":2,"positionName":"环境工程师","createTime":"2024-01-20 22:24:09","modifyTime":"2024-08-10 22:24:09"},
    {"id":4,"enterpriseId":2,"enterpriseName":"蓝天环保有限公司","employeeNumber":"LTHB004","name":"陈静","password":"chenjing123","gender":2,"birthDate":"1991-05-01","contactPhone":"13687654321","email":"chenjing@bluesky-env.com","province": "31", "provinceName": "上海市", "city": "3101", "cityName": "上海市", "area": "310115", "areaName": "浦东新区","address":"张江高科技园区789号","hireDate":"2024-09-05","departmentId":4,"departmentName":"客户服务部","positionId":2,"positionName":"客户服务经理","createTime":"2024-08-25 22:24:09","modifyTime":"2024-12-31 22:24:09"},
    {"id":5,"enterpriseId":2,"enterpriseName":"蓝天环保有限公司","employeeNumber":"LTHB005","name":"刘洋","password":"liuyang456","gender":1,"birthDate":"1989-02-26","contactPhone":"13512345678","email":"liuyang@bluesky-env.com","province": "31", "provinceName": "上海市", "city": "3101", "cityName": "上海市", "area": "310115", "areaName": "浦东新区","address":"张江高科技园区101号","hireDate":"2024-11-16","departmentId":1,"departmentName":"市场营销部","positionId":2,"positionName":"市场专员","createTime":"2024-06-08 22:24:09","modifyTime":"2024-05-13 22:24:09"}
  ]
  constructor() {
  }

  save (employeeForm: EmployeeForm): Observable<Result<void>> {
    console.log('employeeApi save params', employeeForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (employeeQueryForm: EmployeeQueryForm): Observable<Result<Page<Employee>>> {
    console.log('employeeApi find params', employeeQueryForm)
    return MockApi.queryPageSuccessfully(this.employees).pipe(
      map((response: Response<Result<Page<Employee>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<Employee>> {
    console.log('employeeApi findById id', id)
    let target = {}
    for (const a of this.employees) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<Employee>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('employeeApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
